<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns="http://relaxng.org/ns/structure/1.0"
	xmlns:rng="http://relaxng.org/ns/structure/1.0"
	xmlns:table="http://openoffice.org/2000/table"
	version="1.0"
	xmlns:exsl="http://exslt.org/common" 
	extension-element-prefixes="exsl" 
	exclude-result-prefixes = "exsl rng table"
	xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
	>
	 <xsl:strip-space elements="*"/>
   <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
	 
	<xsl:template match="/">
		<grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
			<xsl:apply-templates select="//table:table-row[table:table-cell='root']"/>
		</grammar>
	</xsl:template>

	<xsl:template match="table:table-row[normalize-space(table:table-cell[3])='root']">
		<start>
			<xsl:call-template name="element"/>
		</start>
	</xsl:template>
	
	<xsl:template match="table:table-row[normalize-space(table:table-cell[3])='attribute']" name="attribute">
		<attribute name="{table:table-cell[1]}">
			<xsl:apply-templates select="table:table-cell[2]" mode="step">
				<xsl:with-param name="col" select="2"/>
			</xsl:apply-templates>
		</attribute>
	</xsl:template>
	
	<xsl:template match="table:table-row[normalize-space(table:table-cell[3])='element']" name="element">
		<element name="{table:table-cell[1]}">
			<xsl:apply-templates select="table:table-cell[2]" mode="step">
				<xsl:with-param name="col" select="2"/>
			</xsl:apply-templates>
		</element>
	</xsl:template>
	
	<xsl:template match="table:table-cell" mode="step">
		<xsl:param name="col"/>
		<xsl:choose>
			<xsl:when test="$col=2 and normalize-space(.)">
				<a:documentation>
					<xsl:value-of select="normalize-space(.)"/>
				</a:documentation>
				<xsl:apply-templates select="following-sibling::table:table-cell[1]" mode="step">
					<xsl:with-param name="col" select="$col + 1"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:when test="$col=5 and normalize-space(.)">
				<data type="{normalize-space(.)}"/>
				<xsl:apply-templates select="following-sibling::table:table-cell[1]" mode="step">
					<xsl:with-param name="col" select="$col + 1"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:when test="$col=6 and normalize-space(.)">
				<xsl:call-template name="children"/>
			</xsl:when>
			<xsl:when test="$col>=6"/>
			<xsl:when test="@table:number-columns-repeated">
				<xsl:apply-templates select="following-sibling::table:table-cell[1]" mode="step">
					<xsl:with-param name="col" select="$col + @table:number-columns-repeated"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="following-sibling::table:table-cell[1]" mode="step">
					<xsl:with-param name="col" select="$col + 1"/>
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="children">
		<xsl:param name="list" select="concat(normalize-space(translate(., ',', '')), ' ')"/>
		<xsl:if test="contains($list, ' ')">
			<xsl:apply-templates select="//table:table-row[table:table-cell[1]=substring-before($list, ' ')]" mode="occurs"/>
			<xsl:call-template name="children">
				<xsl:with-param name="list" select="substring-after($list, ' ')"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

	<xsl:template match="table:table-row[normalize-space(table:table-cell[4])='+']" mode="occurs">
		<oneOrMore>
			<xsl:apply-templates select="."/>
		</oneOrMore>
	</xsl:template>
	<xsl:template match="table:table-row[normalize-space(table:table-cell[4])='?']" mode="occurs">
		<optional>
			<xsl:apply-templates select="."/>
		</optional>
	</xsl:template>

	<xsl:template match="table:table-row[normalize-space(table:table-cell[4])='*']" mode="occurs">
		<zeroOrMore>
			<xsl:apply-templates select="."/>
		</zeroOrMore>
	</xsl:template>

	<xsl:template match="table:table-row" mode="occurs">
		<xsl:apply-templates select="."/>
	</xsl:template>

</xsl:stylesheet>
